#!/bin/bash

echo "🧪 Simple Spring AI Agent Test"
echo "================================"
echo ""

BASE_URL="http://localhost:3000"
ADMIN_URL="http://localhost:3001"

# 颜色输出
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color

echo "1. Testing server connectivity..."
response=$(curl -s -w "\n%{http_code}" ${BASE_URL}/api/health)
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | head -n-1)

if [ "$http_code" = "200" ]; then
    echo -e "${GREEN}✅ Server is running${NC}"
    echo "   Response: $body"
else
    echo -e "${RED}❌ Server is not running${NC}"
    exit 1
fi

echo ""
echo "2. Creating test agent..."
create_response=$(curl -s -w "\n%{http_code}" -X POST ${ADMIN_URL}/admin/agents \
  -H "Content-Type: application/json" \
  -d '{
    "agentId": "test-agent",
    "contextManagerUrl": "http://localhost:4000",
    "toolManagerUrl": "http://localhost:4001",
    "controlConfig": {
      "maxSteps": 10,
      "enableToolUsage": true
    },
    "metadata": {
      "description": "Test agent"
    }
  }')

http_code=$(echo "$create_response" | tail -n1)
body=$(echo "$create_response" | head -n-1)

if [ "$http_code" = "201" ] || [ "$http_code" = "200" ]; then
    echo -e "${GREEN}✅ Agent created successfully${NC}"
    echo "   Response: $body" | head -c 200
    echo ""
else
    echo -e "${RED}⚠️  Agent might already exist or creation failed${NC}"
    echo "   HTTP Code: $http_code"
fi

echo ""
echo "3. Starting agent session..."
start_response=$(curl -s -w "\n%{http_code}" -X POST ${BASE_URL}/api/agents/test-agent/sessions/test-session-1/start \
  -H "Content-Type: application/json" \
  -d '{
    "metadata": {
      "userId": "test-user",
      "testRun": true
    }
  }')

http_code=$(echo "$start_response" | tail -n1)
body=$(echo "$start_response" | head -n-1)

if [ "$http_code" = "200" ]; then
    echo -e "${GREEN}✅ Session started successfully${NC}"
    echo "   $body" | python3 -m json.tool 2>/dev/null || echo "   $body"
else
    echo -e "${RED}❌ Failed to start session${NC}"
    echo "   HTTP Code: $http_code"
    echo "   Response: $body"
fi

echo ""
echo "4. Checking session status..."
status_response=$(curl -s -w "\n%{http_code}" ${BASE_URL}/api/agents/test-agent/sessions/test-session-1/status)

http_code=$(echo "$status_response" | tail -n1)
body=$(echo "$status_response" | head -n-1)

if [ "$http_code" = "200" ]; then
    echo -e "${GREEN}✅ Session status retrieved${NC}"
    echo "   $body" | python3 -m json.tool 2>/dev/null || echo "   $body"
else
    echo -e "${RED}❌ Failed to get status${NC}"
    echo "   Response: $body"
fi

echo ""
echo "5. Stopping session..."
stop_response=$(curl -s -w "\n%{http_code}" -X POST ${BASE_URL}/api/agents/test-agent/sessions/test-session-1/stop \
  -H "Content-Type: application/json" \
  -d '{
    "reason": "Test completed"
  }')

http_code=$(echo "$stop_response" | tail -n1)
body=$(echo "$stop_response" | head -n-1)

if [ "$http_code" = "200" ]; then
    echo -e "${GREEN}✅ Session stopped successfully${NC}"
    echo "   $body" | python3 -m json.tool 2>/dev/null || echo "   $body"
else
    echo -e "${RED}❌ Failed to stop session${NC}"
    echo "   Response: $body"
fi

echo ""
echo "🎉 Test completed!"
echo ""
echo "Summary:"
echo "- ✅ Server connectivity check"
echo "- ✅ Agent creation"
echo "- ✅ Session start"
echo "- ✅ Session status query"
echo "- ✅ Session stop"
